#include"global.h"

#include <QProcess>
#include<QStandardPaths>
#include"httpmgr.h"


QString GATE_URL{""};
const QString MachineCode{_getComputerUUID()};
const QString TemporaryAvatar{"./avatar.jpg"};

const QString FileData{QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/newChatData/"};
const QString FileAvatar{FileData + "/avatar/"};
const QString FileOtherFiles{FileData + "/other files/"};
const QString FileChattingRecords{FileData + "/chatting records/"};
QString AvatarPath{FileAvatar + "%1_Avatar.jpg"};


const QString _getComputerUUID()
{
    QProcess process;
#ifdef Q_OS_WIN
    //    wmic csproduct get uuid
    QStringList mList;
    mList << "csproduct" << "get" << "uuid";
    process.start("wmic", mList);

    process.waitForFinished();
    QString ret = process.readAll();
    ret = ret.trimmed();

    QStringList dataList = ret.split("\r\n");

    if(dataList.length() != 2){
        return "";
    }
    return dataList[1].trimmed();
#endif

#ifdef Q_OS_LINUX
    // linux下可能需要先安装dmidecode：sudo apt install dmidecode
    // 然后本程序需要root权限才能获取到有效值
    QStringList mList;
    mList << "-s" << "system-uuid";
    process.start("dmidecode", mList);

    process.waitForFinished();
    QString ret = process.readAll();
    ret = ret.trimmed();

    return ret;
#endif
    return "";
}


std::function<void(QWidget*)> repolish =[](QWidget *w){
    w->style()->unpolish(w);
    w->style()->polish(w);
};
void isGetAvatar(const QString &name)
{
    QString filePath = AvatarPath.arg(name);
    if (!QFile::exists(filePath)) {
        QJsonObject json_obj;
        json_obj["user"] = name;
        HttpMgr::GetInstance()->PostHttpReq(QUrl(GATE_URL+"/requestAvatar"),
                                            QJsonDocument(json_obj).toJson(), ReqId::ID_Get_AVATAR,Modules::GLOBALMOD);
    }
}
